/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbeans;

import java.util.ArrayList;
import javax.faces.model.SelectItem;
import controller.PlanoControllerLocal;
import entities.Plano;
import exception.PlanoExistsException;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIData;
import static utils.MessagesUtil.*;

/**
 *
 * @author Sostenes, Thiago, Felipe
 */
@ManagedBean
@RequestScoped
public class PlanoBean {

    @EJB
    private PlanoControllerLocal planoController;
    private Plano plano;
    private List<Plano> planos;
    private UIData table;


//    public List<Plano> getPlanos() {
//        //planos = planoController.list();
//        return planos;
//    }

    @PostConstruct
    public void init() {
        plano = planoController.createPlano();
    }

    public String create() {
        try {
            planoController.persist(plano);
        } catch (PlanoExistsException e) {
            addErrorMessage(e, "createPlano:nome");
            return "createPlano";
        }
        return "listPlano";
    }

    public String remove() {
        planoController.remove((Plano) table.getRowData());
        return "listPlano";
    }

    public Plano getPlano() {
        return plano;
    }

    public List<Plano> getPlanos() {
        planos = planoController.list();
        return planos;
    }

    public UIData getTable() {
        return this.table;
    }

    public void setTable(UIData table) {
        this.table = table;
    }

    @PreDestroy
    public void clear() {
        plano = null;
        planoController = null;
    }
}
